package cn.ysnk.clz.util;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.kit.PropKit;

import cn.ysnk.clz.entity.generatorCode.EntityColumnEntity;
import cn.ysnk.generateCode.ColumnEnum;
import cn.ysnk.generateCode.MyStringUtil;
import freemarker.template.Configuration;

public class GeneratorUtil {
	
	private static Map<String, String> adminTables = new HashMap<>() ;
	private static String SHOW_TABLES = "show tables";
	private static String SHOW_COLUNM_INFO = "select column_name,column_type,IS_NULLABLE,COLUMN_KEY,COLUMN_COMMENT from INFORMATION_SCHEMA.Columns where table_name='GENERATOR_TABLE_NAME' ";
	private static Configuration config;
	private static Connection conn;
	static{
		try {
			config = new Configuration();
			PropKit.use("a_little_config.txt");
			Class.forName("com.mysql.jdbc.Driver");
			config.setDirectoryForTemplateLoading(new File(MyStringUtil.getTemplatePath()));
			conn = DriverManager.getConnection(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		adminTables.put("clz_user", "clz_user");
		adminTables.put("clz_menu", "clz_menu");
		adminTables.put("clz_role", "clz_role");
		adminTables.put("clz_sub_menu", "clz_sub_menu");
	}
	
	public static List<String> getAllTables() throws SQLException{
		PreparedStatement colunmsPs = conn.prepareStatement(SHOW_TABLES);
		List<String> tables = new ArrayList<String>();
		ResultSet tablesRs = colunmsPs.executeQuery();
		// 获取对应表中的字段
		while (tablesRs.next()) {
			String tableName = tablesRs.getString(1);
			if(!adminTables.containsKey(tableName))
				tables.add(tableName);
		}
		return tables;
	}
	
	public static List<EntityColumnEntity> getTabelInfo(String tabelName) throws SQLException{
		PreparedStatement colunmsPs = conn.prepareStatement(SHOW_COLUNM_INFO.replace("GENERATOR_TABLE_NAME", tabelName));
		List<EntityColumnEntity> colunms = new ArrayList<EntityColumnEntity>();
		ResultSet columnsRs = colunmsPs.executeQuery();
		// 获取对应表中的字段
		while (columnsRs.next()) {
			//字段名称
			String columnName = columnsRs.getString(1);
			String type = columnsRs.getString(2);
			type = type.replaceAll("/(/d+/)", "");
			ColumnEnum columnEnum = ColumnEnum.switch2ColumnEnum(type);
			boolean isPrimaryKey = false;
			//是否主键
			String primarykey = columnsRs.getString(4);
			if (StringUtils.isNotEmpty(primarykey) && StringUtils.equals(primarykey, "PRI")) {
				isPrimaryKey = true;
			}
			String commont = columnsRs.getString(5)==null?"":columnsRs.getString(5);
			boolean nullAble = "YES".equals(columnsRs.getString(3));
			EntityColumnEntity entityColumn =  new EntityColumnEntity(isPrimaryKey, commont, columnEnum, columnName,nullAble);
			colunms.add(entityColumn);
		}
		return colunms;
	}
	
}
